package com.app.couponsharelife.ui.view

import android.graphics.Canvas
import android.util.TypedValue
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.app.couponsharelife.ui.listener.RenRenCallback
import com.app.couponsharelife.utils.CardConfig.MAX_SHOW_COUNT
import com.app.couponsharelife.utils.CardConfig.SCALE_GAP
import com.app.couponsharelife.utils.CardConfig.TRANS_Y_GAP

/**
 *Created by xct on 2019/1/8
 *Description:
 */
class TanTanCallback(private var mRv: RecyclerView, private var adapter:RecyclerView.Adapter<RecyclerView.ViewHolder>,
                     private var datas:ArrayList<Any>): RenRenCallback(mRv,adapter,datas) {
    private val MAX_ROTATION = 15

    //2016 12 26 考虑 探探垂直上下方向滑动，不删除卡片，
    //判断 此次滑动方向是否是竖直的 ，水平方向上的误差(阈值，默认我给了50dp)
    private var mHorizontalDeviation: Int = 0
    init {
        mHorizontalDeviation = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50f, mRv.context.resources.displayMetrics).toInt()
    }

    fun getHorizontalDeviation(): Int {
        return mHorizontalDeviation
    }

    fun setHorizontalDeviation(horizontalDeviation: Int): TanTanCallback {
        mHorizontalDeviation = horizontalDeviation
        return this
    }

    override fun getSwipeThreshold(viewHolder: RecyclerView.ViewHolder): Float {
        /*        Log.d("TAG", "getSwipeThreshold() called with: viewHolder.itemView.getX() = [" + viewHolder.itemView.getX() + "]");
        Log.d("TAG", "getSwipeThreshold() called with:  viewHolder.itemView.getWidth() / 2  = [" + viewHolder.itemView.getWidth() / 2 + "]");
        Log.d("TAG", "getSwipeThreshold() called with:  mRv.getX() = [" + mRv.getX() + "]");
        Log.d("TAG", "getSwipeThreshold() called with:  mRv.getWidth() / 2 = [" + mRv.getWidth() / 2 + "]");*/

        return if (isTopViewCenterInHorizontal(viewHolder.itemView)) {
            java.lang.Float.MAX_VALUE
        } else super.getSwipeThreshold(viewHolder)
    }

    override fun getSwipeEscapeVelocity(defaultValue: Float): Float {
        val topView = mRv.getChildAt(mRv.childCount - 1)
        return if (isTopViewCenterInHorizontal(topView)) {
            java.lang.Float.MAX_VALUE
        } else super.getSwipeEscapeVelocity(defaultValue)
    }

    override fun getSwipeVelocityThreshold(defaultValue: Float): Float {

        val topView = mRv.getChildAt(mRv.childCount - 1)
        return if (isTopViewCenterInHorizontal(topView)) {
            java.lang.Float.MAX_VALUE
        } else super.getSwipeVelocityThreshold(defaultValue)
    }

    /**
     * 返回TopView此时在水平方向上是否是居中的
     *
     * @return
     */
    fun isTopViewCenterInHorizontal(topView: View): Boolean {
        /*Log.d("TAG", "getSwipeThreshold() called with: viewHolder.itemView.getX() = [" + topView.x + "]")
        Log.d("TAG", "getSwipeThreshold() called with:  viewHolder.itemView.getWidth() / 2  = [" + topView.width / 2 + "]")
        Log.d("TAG", "getSwipeThreshold() called with:  mRv.getX() = [" + mRv.x + "]")
        Log.d("TAG", "getSwipeThreshold() called with:  mRv.getWidth() / 2 = [" + mRv.width / 2 + "]")*/
        return Math.abs((mRv.width / 2).toFloat() - topView.x - (topView.width / 2).toFloat()) < mHorizontalDeviation
    }


    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
        //super.onSwiped(viewHolder, direction)
        //如果不需要循环删除
        val remove = datas.removeAt(viewHolder.layoutPosition)
        datas.add(0, remove)
        mRv.adapter!!.notifyDataSetChanged()
        viewHolder.itemView.rotation = 0f
        //viewHolder.itemView.alpha=1f
        /*        if (isLeftSwipe){
            Toast.makeText(mRv.getContext(), "左滑删除", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(mRv.getContext(), "右滑删除", Toast.LENGTH_SHORT).show();
        }*/

        //探探只是第一层加了rotate & alpha的操作
        //对rotate进行复位
        //viewHolder.itemView.rotation = 0f

        //自己感受一下吧 Alpha
        /*if (viewHolder is ViewHolder) {
            val holder = viewHolder as ViewHolder
            holder.setAlpha(R.id.iv_love, 0)
            holder.setAlpha(R.id.iv_del, 0)
        }*/

    }

    override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
        //Log.e("swipecard", "onChildDraw()  viewHolder = [$viewHolder], dX = [$dX], dY = [$dY], actionState = [$actionState], isCurrentlyActive = [$isCurrentlyActive]")
        //探探的效果
        val swipValue = Math.sqrt((dX * dX + dY * dY).toDouble())
        var fraction = swipValue / getThreshold(viewHolder)
        //边界修正 最大为1
        if (fraction > 1) {
            fraction = 1.0
        }
        val childCount = recyclerView.childCount
        for (i in 0 until childCount) {
            val child = recyclerView.getChildAt(i)
            //第几层,举例子，count =7， 最后一个TopView（6）是第0层，
            val level = childCount - i - 1
            if (level > 0) {
                //child.alpha=0.5f
                child.scaleX = (1 - SCALE_GAP * level + fraction * SCALE_GAP).toFloat()

                if (level < MAX_SHOW_COUNT - 1) {
                    child.scaleY = (1 - SCALE_GAP * level + fraction * SCALE_GAP).toFloat()
                    child.translationY = (TRANS_Y_GAP * level - fraction * TRANS_Y_GAP).toFloat()
                } else {
                    //child.setTranslationY((float) (mTranslationYGap * (level - 1) - fraction * mTranslationYGap));
                }
                /*if (level==1){
                    var xFraction = dX / getThreshold(viewHolder)
                    //边界修正 最大为1
                    if (xFraction > 1) {
                        xFraction = 1f
                    } else if (xFraction < -1) {
                        xFraction = -1f
                    }
                    //child.rotation = xFraction * MAX_ROTATION
                    //child.alpha=xFraction
                    child.alpha=if (dX > 0) {
                        //露出左边，比心
                        xFraction
                    } else if (dX < 0) {
                        //露出右边，滚犊子
                        -xFraction
                    } else {
                         0.5f
                    }
                }*/
                /*if (viewHolder is ViewHolder) {
                    val holder = viewHolder as ViewHolder
                    if (dX > 0) {
                        //露出左边，比心
                        holder.setAlpha(R.id.iv_love, xFraction)
                    } else if (dX < 0) {
                        //露出右边，滚犊子
                        holder.setAlpha(R.id.iv_del, -xFraction)
                    } else {
                        holder.setAlpha(R.id.iv_love, 0)
                        holder.setAlpha(R.id.iv_del, 0)
                    }


                }*/
            } else {
                //探探只是第一层加了rotate & alpha的操作
                //不过他区分左右
                var xFraction = dX / getThreshold(viewHolder)
                //边界修正 最大为1
                if (xFraction > 1) {
                    xFraction = 1f
                } else if (xFraction < -1) {
                    xFraction = -1f
                }
                //rotate
                child.rotation = xFraction * MAX_ROTATION
                //自己感受一下吧 Alpha
                /*if (viewHolder is ViewHolder) {
                    val holder = viewHolder as ViewHolder
                    if (dX > 0) {
                        //露出左边，比心
                        holder.setAlpha(R.id.iv_love, xFraction)
                    } else if (dX < 0) {
                        //露出右边，滚犊子
                        holder.setAlpha(R.id.iv_del, -xFraction)
                    } else {
                        holder.setAlpha(R.id.iv_love, 0)
                        holder.setAlpha(R.id.iv_del, 0)
                    }


                }*/
            }
        }


        //可以在此判断左右滑：
        val v = mRv.width / 2 - viewHolder.itemView.x - viewHolder.itemView.width / 2
        if (v > 0) {
            isLeftSwipe = true
        } else if (v < 0) {
            isLeftSwipe = false
        }
    }

    //一个flag 判断左右滑
    private var isLeftSwipe: Boolean = false
}